/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.easoft.code.pos.ui.beans;

import com.easoft.code.pos.App;
import com.easoft.code.pos.ui.beans.pos.POSOperation;
import com.easoft.code.pos.util.DataFormatter;
import com.easoft.code.pos.util.UIUtil;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Point;

/**
 *
 * @author Eliu
 */
public class FormPOSFormaPago extends javax.swing.JDialog {

    private POSOperation operation;

    public POSOperation getOperation() {
        return operation;
    }

    public void setOperation(POSOperation operation) {
        this.operation = operation;
    }

    /**
     * Creates new form FormPOSFormaPago
     *
     * @param operation
     */
    public FormPOSFormaPago(POSOperation operation) {
        initComponents();
        this.operation = operation;
        this.operation.setTotalPagado(0d);
        this.operation.setTotal(Double.valueOf(DataFormatter.formatNumber(this.operation.getTotal())));
        this.txtTotalFactura.setText(DataFormatter.formatNumber(this.operation.getTotal()));
        Frame parent = App.mainForm;
        if (parent != null) {
            Dimension parentSize = parent.getSize();
            Point p = parent.getLocation();
            setLocation(p.x + parentSize.width / 4, p.y + parentSize.height / 4);
        }
        setSize(new Dimension(380, 380));
        setVisible(true);
    }

    private void updateTotal() {
        double efec, tar, cred, cambio;
        cambio = efec = tar = cred = 0d;
        double total = this.operation.getTotal();
        txtCambio.setText("0.00");
        try {
            efec = Double.valueOf((!txtEfectivo.getText().isEmpty()
                    ? txtEfectivo.getText() : "0"));
            if (efec > total) {
                cambio = efec - total;
                txtCambio.setText(DataFormatter.formatNumber(cambio));
            }
            operation.setEfectivo(efec);
        } catch (NumberFormatException e) {
            txtEfectivo.setText("0.00");
            return;
        }
        try {
            cred = Double.valueOf((!txtCredito.getText().isEmpty()
                    ? txtCredito.getText() : "0"));
            operation.setCredito(cred);
        } catch (NumberFormatException e) {
            txtCredito.setText("0.00");
            return;
        }
        try {
            tar = Double.valueOf((!txtTarjeta.getText().isEmpty()
                    ? txtTarjeta.getText() : "0"));
            operation.setTarjeta(tar);
        } catch (NumberFormatException e) {
            txtTarjeta.setText("0.00");
            return;
        }
        this.operation.setTotalPagado(efec + tar + cred);
        this.operation.setCambio(cambio);
        txtTotal.setText(DataFormatter.formatNumber(this.operation.getTotalPagado()));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel9 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel8 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtEfectivo = new javax.swing.JTextField();
        txtTarjeta = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtCredito = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtTotal = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtCambio = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtTotalFactura = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jPanel10 = new javax.swing.JPanel();
        btnContinuar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setModal(true);
        setResizable(false);

        jPanel9.setPreferredSize(new java.awt.Dimension(475, 30));
        jPanel9.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel1.setText("Especificacion forma de pago:");
        jPanel9.add(jLabel1);

        getContentPane().add(jPanel9, java.awt.BorderLayout.PAGE_START);

        jPanel8.setPreferredSize(new java.awt.Dimension(475, 200));
        jPanel8.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel2.setText("Efectivo");
        jPanel8.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, -1, -1));

        txtEfectivo.setText("0.00");
        txtEfectivo.setNextFocusableComponent(txtTarjeta);
        txtEfectivo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtEfectivoKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtEfectivoKeyTyped(evt);
            }
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtEfectivoKeyPressed(evt);
            }
        });
        jPanel8.add(txtEfectivo, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 40, 130, -1));

        txtTarjeta.setText("0.00");
        txtTarjeta.setNextFocusableComponent(txtCredito);
        txtTarjeta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtTarjetaActionPerformed(evt);
            }
        });
        txtTarjeta.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtTarjetaKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtTarjetaKeyTyped(evt);
            }
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtTarjetaKeyPressed(evt);
            }
        });
        jPanel8.add(txtTarjeta, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 70, 130, -1));

        jLabel3.setText("Tarjeta");
        jPanel8.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 70, -1, -1));

        txtCredito.setText("0.00");
        txtCredito.setNextFocusableComponent(btnContinuar);
        txtCredito.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtCreditoKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtCreditoKeyTyped(evt);
            }
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtCreditoKeyPressed(evt);
            }
        });
        jPanel8.add(txtCredito, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 100, 130, -1));

        jLabel4.setText("Credito");
        jPanel8.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 100, -1, -1));

        jLabel5.setText("Total");
        jPanel8.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 130, -1, -1));

        txtTotal.setEditable(false);
        txtTotal.setText("0.00");
        jPanel8.add(txtTotal, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 130, 130, -1));

        jLabel6.setText("Cambio");
        jPanel8.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 160, -1, -1));

        txtCambio.setEditable(false);
        txtCambio.setText("0.00");
        jPanel8.add(txtCambio, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 160, 130, -1));

        jLabel7.setText("Valor Facturado");
        jPanel8.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, -1, -1));

        txtTotalFactura.setEditable(false);
        txtTotalFactura.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        txtTotalFactura.setText("0.00");
        txtTotalFactura.setNextFocusableComponent(txtTarjeta);
        txtTotalFactura.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtTotalFacturaKeyPressed(evt);
            }
        });
        jPanel8.add(txtTotalFactura, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 10, 110, -1));

        jButton1.setText("Usar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel8.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 100, -1, -1));

        jButton2.setText("Usar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel8.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 40, -1, -1));

        jButton3.setText("Usar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel8.add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 70, -1, -1));

        getContentPane().add(jPanel8, java.awt.BorderLayout.CENTER);

        jPanel10.setPreferredSize(new java.awt.Dimension(475, 40));

        btnContinuar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/tick.png"))); // NOI18N
        btnContinuar.setText("Continuar");
        btnContinuar.setNextFocusableComponent(btnCancelar);
        btnContinuar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnContinuarActionPerformed(evt);
            }
        });
        jPanel10.add(btnContinuar);

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon-16-delete.png"))); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });
        jPanel10.add(btnCancelar);

        getContentPane().add(jPanel10, java.awt.BorderLayout.PAGE_END);
    }// </editor-fold>//GEN-END:initComponents

    private void btnContinuarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnContinuarActionPerformed
        if (!operation.validPayment()) {
            UIUtil.showError("El total pagado debe ser igual al total facturado!", "Totalizacion Pago");
        } else {
            operation.setCompletado(true);
            setVisible(false);
        }
    }//GEN-LAST:event_btnContinuarActionPerformed

    private void txtTarjetaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtTarjetaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtTarjetaActionPerformed

    private void txtEfectivoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtEfectivoKeyPressed
    }//GEN-LAST:event_txtEfectivoKeyPressed

    private void txtTarjetaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtTarjetaKeyPressed
    }//GEN-LAST:event_txtTarjetaKeyPressed

    private void txtCreditoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCreditoKeyPressed
    }//GEN-LAST:event_txtCreditoKeyPressed

    private void txtTotalFacturaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtTotalFacturaKeyPressed
    }//GEN-LAST:event_txtTotalFacturaKeyPressed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        operation.setCompletado(false);
        setVisible(false);
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void txtEfectivoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtEfectivoKeyTyped
    }//GEN-LAST:event_txtEfectivoKeyTyped

    private void txtTarjetaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtTarjetaKeyTyped
    }//GEN-LAST:event_txtTarjetaKeyTyped

    private void txtCreditoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCreditoKeyTyped
    }//GEN-LAST:event_txtCreditoKeyTyped

    private void txtEfectivoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtEfectivoKeyReleased
        updateTotal();
    }//GEN-LAST:event_txtEfectivoKeyReleased

    private void txtTarjetaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtTarjetaKeyReleased
        updateTotal();
    }//GEN-LAST:event_txtTarjetaKeyReleased

    private void txtCreditoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCreditoKeyReleased
        updateTotal();
    }//GEN-LAST:event_txtCreditoKeyReleased

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        txtEfectivo.setText(txtTotalFactura.getText());
        updateTotal();
        btnContinuarActionPerformed(null);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        txtTarjeta.setText(txtTotalFactura.getText());
        updateTotal();
        btnContinuarActionPerformed(null);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        txtCredito.setText(txtTotalFactura.getText());
        updateTotal();
        btnContinuarActionPerformed(null);
    }//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnContinuar;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JTextField txtCambio;
    private javax.swing.JTextField txtCredito;
    private javax.swing.JTextField txtEfectivo;
    private javax.swing.JTextField txtTarjeta;
    private javax.swing.JTextField txtTotal;
    private javax.swing.JTextField txtTotalFactura;
    // End of variables declaration//GEN-END:variables
}
